package sfeir.ergosum.tools;

import java.util.ArrayList;

import sfeir.ergosum.HiveActivity;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Window;

import com.sfeir.proto.hivelayout.HiveAction;
import com.sfeir.proto.hivelayout.HiveActionCell;
import com.sfeir.proto.hivelayout.HiveMenuView;

public class HiveColorPickerActivity extends Activity implements ColorPickerDialog.OnColorChangedListener{

    private static final String NAME = HiveActivity.PREFS_NAME;

    private HiveMenuView hmv;
    private HiveActionCell c1;
    private HiveActionCell c2;
    private HiveActionCell c3;
    private HiveActionCell c4;
    private HiveActionCell c5;
    private HiveActionCell c6;
    private HiveActionCell cs;

    private ArrayList<Integer> currentColors;
    private ArrayList<HiveAction> actionCellColor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        init();

        hmv = new HiveMenuView(this);
        int height = getWindow().getWindowManager().getDefaultDisplay().getHeight();
        int width = getWindow().getWindowManager().getDefaultDisplay().getWidth();

        // Less statusBar's size
        hmv.setConstants(width, height-25);
        hmv.setBackgroundColor(0x40000000);

        for(int i=1; i<6; i++) {
            String text = i<4?"Hive ":"Menu ";
            HiveAction actionColor = new HiveAction(text+i) {
                public void executeAction() {
                    int color = (Integer)getObjects().get("color");
                    int position = (Integer)getObjects().get("position");
                    new ColorPickerDialog(
                            HiveColorPickerActivity.this,
                            HiveColorPickerActivity.this,
                            color, position).show();
                }
            };
            actionColor.getObjects().put("color", currentColors.get(i-1));
            actionColor.getObjects().put("position", i);
            actionCellColor.add(actionColor);
        }

        c1 = new HiveActionCell(HiveColorPickerActivity.this, actionCellColor.get(0));
        c1.setShown(true);
        c1.setCellColor(currentColors.get(0));
        c1.setEmboss(true);
        hmv.setHiveMenuCell2(c1);

        c2 = new HiveActionCell(HiveColorPickerActivity.this, actionCellColor.get(1));
        c2.setShown(true);
        c2.setCellColor(currentColors.get(1));
        c2.setEmboss(true);
        hmv.setHiveMenuCell3(c2);

        c3 = new HiveActionCell(HiveColorPickerActivity.this, actionCellColor.get(2));
        c3.setShown(true);
        c3.setCellColor(currentColors.get(2));
        c3.setEmboss(true);
        hmv.setHiveMenuCell4(c3);

        c4 = new HiveActionCell(HiveColorPickerActivity.this, actionCellColor.get(3));
        c4.setShown(true);
        c4.setCellColor(currentColors.get(3));
        c4.setEmboss(true);
        hmv.setHiveMenuCell5(c4);

        c5 = new HiveActionCell(HiveColorPickerActivity.this, actionCellColor.get(4));
        c5.setShown(true);
        c5.setCellColor(currentColors.get(4));
        c5.setEmboss(true);
        hmv.setHiveMenuCell6(c5);

        HiveAction actionDefault = new HiveAction("DEFAULT") {
            public void executeAction() {
                setDefault();
            };
        };
        c6 = new HiveActionCell(HiveColorPickerActivity.this, actionDefault);
        c6.setShown(true);
        c6.setCellColor(0xFF000000);
        c6.setEmboss(true);
        hmv.setHiveMenuCell1(c6);

        HiveAction actionSave = new HiveAction("SAVE") {
            public void executeAction() {
                save();
            };
        };
        cs = new HiveActionCell(HiveColorPickerActivity.this, actionSave);
        cs.setShown(true);
        cs.setCellColor(0xFF000000);
        cs.setEmboss(true);
        hmv.setHiveStatusMenuCell(cs);

        hmv.setup();
        setContentView(hmv);
    }

    private void init() {
        actionCellColor = new ArrayList<HiveAction>();
        currentColors = new ArrayList<Integer>();

        

        ThemeColor theme = new ThemeColor(this);
        for(int c: theme.getCellColors()) {
            currentColors.add(c);
        }
        for(int c: theme.getMenuColors()) {
            currentColors.add(c);
        }
    }

    private void save() {
        SharedPreferences settings = getSharedPreferences(NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("preference_theme", "customized");

        currentColors.clear();
        currentColors.add(c1.getCellColor());
        currentColors.add(c2.getCellColor());
        currentColors.add(c3.getCellColor());
        currentColors.add(c4.getCellColor());
        currentColors.add(c5.getCellColor());

        editor.putInt("color1", currentColors.get(0));
        editor.putInt("color2", currentColors.get(1));
        editor.putInt("color3", currentColors.get(2));
        editor.putInt("color4", currentColors.get(3));
        editor.putInt("color5", currentColors.get(4));

        // Don't forget to commit edits!!!
        editor.commit();
    }

    private void setDefault() {
        c1.setCellColor(currentColors.get(0));
        c2.setCellColor(currentColors.get(1));
        c3.setCellColor(currentColors.get(2));
        c4.setCellColor(currentColors.get(3));
        c5.setCellColor(currentColors.get(4));
    }

    @Override
    public void colorChanged(int color, int position) {
        switch (position) {
        case 1:
            c1.setCellColor(color);
            c1.invalidate();
            break;
        case 2:
            c2.setCellColor(color);
            c2.invalidate();
            break;
        case 3:
            c3.setCellColor(color);
            c3.invalidate();
            break;
        case 4:
            c4.setCellColor(color);
            c4.invalidate();
            break;
        case 5:
            c5.setCellColor(color);
            c5.invalidate();
            break;
        case 6:
            c6.setCellColor(color);
            c6.invalidate();
            break;
        default:
            break;
        }
    }
}
